/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.fx.client.channel.StyleChannel;
import me.l1k3.fx.client.channel.inter.DimensionConstant;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Unit;

public class FXBox extends FXChannel {
    
    
	//- - - factory
	
	public final static FXBox box(Element element) {
	    FXBox box = new FXBox();
	    box.init(element);
	    box.box(DimensionConstant.Box.RESIZE, DimensionConstant.AnchorHorizontal.CENTER, DimensionConstant.Box.RESIZE, DimensionConstant.AnchorVertical.CENTER);
	    
	    return box;
	}
	
    public final static FXBox vertical(Element element) {
        FXBox box = new FXBox();
        box.init(element);
        box.box(DimensionConstant.Box.NO_RESIZE, DimensionConstant.AnchorHorizontal.CENTER, DimensionConstant.Box.RESIZE, DimensionConstant.AnchorVertical.TOP);
    
        return box;
    }
    
    public final static FXBox horizontal(Element element) {
        FXBox box = new FXBox();
        box.init(element);
        box.box(DimensionConstant.Box.RESIZE, DimensionConstant.AnchorHorizontal.LEFT, DimensionConstant.Box.NO_RESIZE, DimensionConstant.AnchorVertical.CENTER);
    
        return box;
    }
	
	//- - -
        
    public void box(DimensionConstant.Box horizontal, DimensionConstant.AnchorHorizontal AnchorHorizontal, DimensionConstant.Box vertical, DimensionConstant.AnchorVertical AnchorVertical) {
        Element element = getContext().getElement();
        
        int width = element.getOffsetWidth();
        int height = element.getOffsetHeight();
        
        if(horizontal==DimensionConstant.Box.RESIZE) {
            setWidth(width, 0);
        }
        
        if(horizontal==DimensionConstant.Box.RESIZE || horizontal==DimensionConstant.Box.NO_RESIZE) {
            if(AnchorHorizontal==DimensionConstant.AnchorHorizontal.CENTER) {
                setX(0, width/2);
            }
            else
            if(AnchorHorizontal==DimensionConstant.AnchorHorizontal.RIGHT) {
                setX(0, width);
            }
            if(horizontal==DimensionConstant.Box.NO_RESIZE && AnchorHorizontal==DimensionConstant.AnchorHorizontal.LEFT) {
                setX(0, -width);
            }
        }
        
        if(vertical==DimensionConstant.Box.RESIZE) {
            setHeight(height, 0);
        }
        
        if(vertical==DimensionConstant.Box.RESIZE || vertical==DimensionConstant.Box.NO_RESIZE) {
            if(AnchorVertical==DimensionConstant.AnchorVertical.CENTER) {
                setY(0, height/2);
            }
            else
            if(AnchorVertical==DimensionConstant.AnchorVertical.BOTTOM) {
                setY(0, height);
            }
            if(vertical==DimensionConstant.Box.NO_RESIZE && AnchorVertical==DimensionConstant.AnchorVertical.TOP) {
                setY(0, -height);
            }
        }
        
        setHideAndShow((horizontal==DimensionConstant.Box.RESIZE || vertical==DimensionConstant.Box.RESIZE), false);
    }
	
	public final void setX(int start, int end) {
		add(StyleChannel.setStyle(getType(), "left", start, end, Unit.PX));
	}
	
	public final void setY(int start, int end) {
		add(StyleChannel.setStyle(getType(), "top", start, end, Unit.PX));
	}

	public final void setWidth(int start, int end) {
		add(StyleChannel.setStyle(getType(), "width", start, end, Unit.PX));
	}

	public final void setHeight(int start, int end) {
		add(StyleChannel.setStyle(getType(), "height", start, end, Unit.PX));
	}
}
